<?php
namespace plugins\store\controller;
use cmf\controller\PluginAdminBaseController;
use think\Db;use app\admin\model\RoleModel;
use plugins\store\model\store_rbac\StoreRbacModel;
use plugins\store\Common as STORECOMMON;
use app\admin\model\AuthAccessModel;
use plugins\store\model\StoreMemberAdminModel;

/** *店铺角色管理  **/
class StoreRbacController extends PluginAdminBaseController
{	
	protected function _initialize()	
	{	

	}  

	//首页	
	public function index()	
	{	    
		$storeRelevance = STORECOMMON::getStoreRelevanceInfo();
		//获取当前登录用户的关联信息	    
		$roleList = StoreRbacModel::getStoreRoleList( $storeRelevance->store_id , function( $item ){	        
			$nbsp = '&nbsp;&nbsp;&nbsp;&nbsp;';	        
			$style ='style="color:green;"';	        
			$pro = $style.' data-tag="'.$item->id.'"';	        
			$btns   = $nbsp.'<i '.$pro.' class="layui-icon btn-user" title="用户">&#xe66f;'.count($this->showUser( $item->id , false )).'</i>';	        
			$btns   .= $nbsp.'<i '.$pro.' class="layui-icon btn-add" title="添加角色">&#xe654;</i> ';	        
			if( false === strpos( $item ['remark'] , 'THISROLEIS_SPGL') ){	            
				$btns  .= $nbsp.'<i '.$pro.' data-name="'.$item->name.'" class="layui-icon btn-edit" title="修改角色">&#xe642;</i> ';    	        
				$btns  .= $nbsp.'<i '.$pro.' class="layui-icon btn-del" title="删除角色">&#xe640;</i> ';    	        
				$btns  .= $nbsp.'<i '.$pro.' class="layui-icon btn-rbac" title="权限设置">&#xe672;</i> ';	        
			}	        
			return $btns;	    
		});	    
		/*dump(json_encode( $roleList ,JSON_UNESCAPED_UNICODE ) );*/	    
		$this->assign( 'rbacData' , json_encode( $roleList , JSON_UNESCAPED_UNICODE ) );	    
		return $this->fetch(  );	
	}	

	//添加店铺管理角色	
	public function addRolePost()	
	{	    
		$param = $this->request->param();	    
		/**	    *角色信息存入  cmf_role 表 	    * company_id 为空  remark 为店铺id S_(id)   部门id为  0	    *	    **/	    
		$storeInfo = STORECOMMON::getStoreInfo();
		//店铺信息	    
		if( $storeInfo ){    	    
			$param ['department_id'] = 0;    	    
			$param ['company_id'] = 0;    	    
			$param ['remark'] = 'S_'.$storeInfo->store_id;    	    
			if( !( new RoleModel () ) -> allowField( true )->isUpdate( false )->save( $param )  ){    	        
				return api_json_echo( false , '添加角色失败' );    	    
			}    	    
			return api_json_echo( true , '添加角色成功' );	    
		}	    
		return api_json_echo( false , '未获取到关联信息' );	
	}	

	//修改店铺管理角色信息	
	public function editRolePost()	
	{	    
		$param = $this->request->param();	    
		if( !( new RoleModel () ) -> allowField( true )->isUpdate( true )->save( $param ) ){	        
			return api_json_echo( false , '修改角色失败' );	    
		}	    
		return api_json_echo( true , '修改角色成功' );	
	}	

	//删除	
	public function delRole()	
	{   	    
		$param = $this->request->param();	    
		$spgl = StoreRbacModel::getSpgjRole();	    
		if( $spgl->id === $param [ 'id' ] ){	        
			return api_json_echo( false , '无法删除基础角色信息' ); 	    
		}	    
		if( StoreMemberAdminModel::where( 'role_id' ,$param['id'] )->count()>0 ){	        
			return api_json_echo( false , '角色下绑定有用户,不能删除' );	    
		}	    
		//删除角色已授权的菜单信息	    
		(new AuthAccessModel())->where( 'role_id' , $param [ 'id' ] )->delete();	    
		if( !empty( $param [ 'id' ] ) && !RoleModel::destroy($param['id']) ){	       
			return api_json_echo( false , '删除失败' ); 	    
		}	    
		return api_json_echo( true , '删除成功' );	
	}	

	//提交授权信息	
	public function checkedRbacPost()	
	{	    
		$param = $this->request->param();	    
		$role = $param[ 'role_id' ];	    
		$list = isset( $param[ 'checkData' ] ) ? $param[ 'checkData' ] : [];	    
		StoreRbacModel::saveRbac( $list , $role );	    
		return api_json_echo( true , '操作成功' );	
	}	

	//设置角色权限	
	public function setRbac()	
	{	    
		$param = $this->request->param();	    
		$roleId = $param[ 'role_id' ];	    
		$baseInfo = StoreRbacModel::getBaseRbacInfo( $roleId );	    
		$this->assign( 'baseInfo' , json_encode( $baseInfo , JSON_UNESCAPED_UNICODE )  );	    
		$this->assign( 'role_id' , $roleId );	    
		return $this->fetch('set_rbac');	
	}	

	//查看用户	
	public function showUser( $id , $isPage = true )	
	{	    
		$param = $this->request->param();	    
		$spgl = STORECOMMON::getSpglRoleInfo();	    
		$ids = StoreMemberAdminModel::where( 'role_id' , $id );	    
		if( $spgl->id == $id ){	       
			$ids = $ids->where( 'user_id' , cmf_get_current_admin_id() );	    
		}	    
		$ids = $ids->column( 'user_id' );	    
		$user = Db( 'user' )->where('id','in',$ids)->field( 'user_login as name,id' )->select( );	    
		if( !$isPage ){	        
			return $user;	    
		}	    
		$this->assign('id',$id);	    
		$this->assign( 'user' , $user );	    
		return $this->fetch( 'user_list' );	
	}
}